// Copyright (c) 2011, Grant China
// All rights reserved.

package org.gwtlcars.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.WidgetCollection;

public class LcarsRadioButtonGroup extends Composite implements HasText {

    private static LcarsRadioButtonGroupUiBinder uiBinder = GWT
            .create(LcarsRadioButtonGroupUiBinder.class);

    interface LcarsRadioButtonGroupUiBinder extends
            UiBinder<Widget, LcarsRadioButtonGroup> {
    }

    @UiField
    DockLayoutPanel dock;

    @UiField
    Label hpanel_spacer;

    @UiField
    FlowPanel button_area;

    @UiField
    FlowPanel vpanel;

    @UiField
    HorizontalPanel hpanel;

    @UiField
    Label lbl_top_left;

    private String color = LcarsColor.DARK_RED;
    private WidgetCollection buttons = new WidgetCollection(button_area);

    public LcarsRadioButtonGroup() {
        initWidget(uiBinder.createAndBindUi(this));
        setColor(color);
    }

    public LcarsRadioButtonGroup(String text) {
        initWidget(uiBinder.createAndBindUi(this));
        hpanel_spacer.setText(text);
        setColor(color);
    }

    public void setColor(String lcars_color) {

        color = lcars_color;
        vpanel.addStyleName(color);
        lbl_top_left.addStyleName(color);
        hpanel_spacer.addStyleName(color);
    }

    @Override
    public String getText() {
        return hpanel_spacer.getText();
    }

    @Override
    public void setText(String text) {
        hpanel_spacer.setText(text);
    }

    public void add(LcarsRadioButton button) {
        button_area.add(button);
        buttons.add(button);
        button.setParent(this);

        int button_count = button_area.getWidgetCount();
        dock.setHeight(button_count * 3.5 + 1.5 + "em");

        // default to first button added enabled
        if (button_count == 1)
            button.setEnabled(true);
    }

    protected WidgetCollection getChildren() {
        return buttons;
    }
}
